package com.livestocksolutions.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.text.AbstractDocument;

import com.livestocksolutions.Chemicine;
import com.livestocksolutions.UsedItem;
import com.livestocksolutions.util.DecimalDocumentFilter;

/**
 * A class that holds similar Components for sub classes of {@link com.livestocksolutions.Chemicine}.
 * @author Adam Parr
 *
 * @param <T> A sub class of Chemicine.
 * @param <A> A sub class of UsedItem which is related to T.
 */
public abstract class ChemicinePanel<T extends Chemicine<A>, A extends UsedItem> extends ItemPanel<T, A> {
	private static final long serialVersionUID = 1L;
	
	private JTextField spinnerWithhold = new JTextField();
	private JComboBox cbWithholdPeriod = new JComboBox();
	private JTextArea txtApplicationMethod = new JTextArea();
	
	/**
	 * Constructor to create a TabbedInterface of a sub class of a UsableItem.
	 * @param type String containing the type of T that will be shown in the GUI.
	 */
	protected ChemicinePanel(String type){
		super(type);
		((AbstractDocument)spinnerWithhold.getDocument()).setDocumentFilter(new DecimalDocumentFilter(0d,Double.POSITIVE_INFINITY));
		cbWithholdPeriod.setModel(new DefaultComboBoxModel(new String[] {Chemicine.getWithholdPeriodUnitString(Chemicine.MINUTE),Chemicine.getWithholdPeriodUnitString(Chemicine.HOUR),Chemicine.getWithholdPeriodUnitString(Chemicine.DAY),Chemicine.getWithholdPeriodUnitString(Chemicine.MONTH),Chemicine.getWithholdPeriodUnitString(Chemicine.YEAR)}));
	}
	
	@Override
	protected boolean doCheck(boolean isValid) {
		return isValid;
	}
	
	@Override
	protected boolean save(T item){
		item.setApplicationDesc(txtApplicationMethod.getText());
		item.setWithholdPeriod(Double.parseDouble(spinnerWithhold.getText()),Chemicine.getWitholdUnitInt((String)cbWithholdPeriod.getSelectedItem()));
		
		return true;
	}
	
	@Override
	protected boolean add(T item, JPanel nameColumn, JPanel valueColumn, JPanel aditional){
		
		JPanel lblWithholdPanel = new JPanel();
		lblWithholdPanel.setBorder(new EmptyBorder(10, 0, 5, 0));
		lblWithholdPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
		nameColumn.add(lblWithholdPanel);
		lblWithholdPanel.add(new JLabel("Withhold Period:"));
		
		JPanel withholdValuePanel = new JPanel();
		withholdValuePanel.setBorder(new EmptyBorder(0, 0, 5, 0));
		valueColumn.add(withholdValuePanel);
		withholdValuePanel.setLayout(new BorderLayout(5, 0));
		withholdValuePanel.add(spinnerWithhold, BorderLayout.CENTER);
		withholdValuePanel.add(cbWithholdPeriod, BorderLayout.EAST);
		
		JPanel applicationPanel = new JPanel();
		aditional.add(applicationPanel, BorderLayout.SOUTH);
		applicationPanel.setLayout(new BorderLayout(0, 5));
		applicationPanel.add(new JLabel("Application Method:"), BorderLayout.NORTH);
		txtApplicationMethod.setRows(3);
		applicationPanel.add(txtApplicationMethod, BorderLayout.CENTER);
 
        spinnerWithhold.setText(item!=null?Double.toString(item.getWithholdPeriod()):"0");
        cbWithholdPeriod.setSelectedIndex(item!=null?item.getWithholdPeriodUnit():Chemicine.DAY);

		// Disable editing if not being edited.
		spinnerWithhold.setEditable(edit);
		cbWithholdPeriod.setEnabled(edit);
		txtApplicationMethod.setEditable(edit);
		
		return true;
	}
}
